package tq.com.tqcom.net;

import org.json.JSONObject;

import java.util.HashMap;

public class TQNetResult {
    private JSONObject data;
    private int ret = 0;
    private String errMsg;

    public String requestUrl;
    static public int RET_SUCCES = 0;

    public TQNetResult(int r, String msg){
        ret = r;
        errMsg = msg;
    }
    public TQNetResult(JSONObject pJSONObject){
        if(pJSONObject == null)return;

        ret = pJSONObject.optInt("ret");
        if(ret == RET_SUCCES){

        }else{
            errMsg = errorInfo.get(ret);
        }
        data = pJSONObject;
    }

    public boolean  isSuccess(){
        return ret == RET_SUCCES;
    }

    public JSONObject getData() {
        return data;
    }

    public int getRet() {
        return ret;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public static int getRetSucces() {
        return RET_SUCCES;
    }


    static public TQNetResult errMsg(int code, String msg){
        return new TQNetResult(code, msg);
    }


    static public TQNetResult TQ_NET_ERROR = errMsg(-1, "网络返回失败");

    static final public int NET_ERROR_SYS = 10000;
    static final public int NET_BAD_REQUEST = 10010;
    static final public int NET_BAD_TOKEN = 10011;
    static final public int NET_INVALID_PWD = 10012;
    static final public int NET_PERMISSION = 10013;
    static final public int NET_HD_FILE = 10020;
    static final public int NET_DB_FILE = 10021;
    static final public int NET_NAME_EXISTS = 10022;

    static private HashMap<Integer, String> errorInfo = new HashMap<>();
    static {
        errorInfo.put(10000, "系统错误");
        errorInfo.put(10010, "暂无数据");
        errorInfo.put(10011, "身份过期或者无效");
        errorInfo.put(10012, "用户名或密码错误");
        errorInfo.put(10013, "禁止操作/权限不足");

        errorInfo.put(10020, "上传文件失败");
        errorInfo.put(10021, "上传文件失败");
        errorInfo.put(10022, "名字重复");
    }
}
